package com.cabal.contribute.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cabal.auth.dao.CabalAuthMapper;
import com.cabal.common.constants.SystemConstants;
import com.cabal.common.util.SnowflakeIdUtil;
import com.cabal.config.DynamicDataSourceContextHolder;
import com.cabal.contribute.dao.CabalContributePointLogMapper;
import com.cabal.contribute.dao.CabalContributePointAccountMapper;
import com.cabal.contribute.entity.CabalContributePointAccount;
import com.cabal.contribute.entity.CabalContributePointLog;
import com.cabal.contribute.service.CabalContributePointService;
import com.cabal.mall.entity.CabalMallCoinAccount;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Slf4j
@Service
public class CabalContributePointServiceImpl implements CabalContributePointService {

    @Resource
    private CabalContributePointLogMapper cabalContributePointLogMapper;

    @Resource
    private CabalContributePointAccountMapper cabalContributePointAccountMapper;


    @Override
    public Integer insertCabalContributePointLog(CabalContributePointLog cabalContributePointLog) {
        QueryWrapper<CabalContributePointAccount> queryWrapper = new QueryWrapper<CabalContributePointAccount>();
        queryWrapper.lambda().eq(CabalContributePointAccount::getUserNum, cabalContributePointLog.getUserNum());
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        CabalContributePointAccount dbCabalContributePointAccount =  cabalContributePointAccountMapper.selectOne(queryWrapper);
        if(dbCabalContributePointAccount == null) {
            cabalContributePointLog.setPointPre(0);
            cabalContributePointLog.setAccount(cabalContributePointLog.getAccount());
            cabalContributePointLog.setPointAfter(cabalContributePointLog.getChangePoint());
        } else {
            cabalContributePointLog.setPointPre(dbCabalContributePointAccount.getContributePoint());
            cabalContributePointLog.setPointAfter(dbCabalContributePointAccount.getContributePoint() + cabalContributePointLog.getChangePoint());
        }

        cabalContributePointLog.setId(SnowflakeIdUtil.getInstance().nextId());
        cabalContributePointLog.setCreateTime(new Timestamp(new Date().getTime()));
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        cabalContributePointLogMapper.insert(cabalContributePointLog);

        return cabalContributePointLog.getPointAfter();
    }

    @Override
    public void insertOrUpdateCabalContributePointAccount(CabalContributePointAccount cabalContributePointAccount) {
        QueryWrapper<CabalContributePointAccount> queryWrapper = new QueryWrapper<CabalContributePointAccount>();
        queryWrapper.lambda().eq(CabalContributePointAccount::getUserNum, cabalContributePointAccount.getUserNum());
        DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
        CabalContributePointAccount dbCabalContributePointAccount =  cabalContributePointAccountMapper.selectOne(queryWrapper);

        if(dbCabalContributePointAccount == null) {
            cabalContributePointAccount.setId(SnowflakeIdUtil.getInstance().nextId());
            cabalContributePointAccount.setCreateTime(new Timestamp(new Date().getTime()));
            cabalContributePointAccount.setUpdateTime(cabalContributePointAccount.getCreateTime());
            cabalContributePointAccount.setAccount(cabalContributePointAccount.getAccount());
            DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
            cabalContributePointAccountMapper.insert(cabalContributePointAccount);
        } else {
            CabalContributePointAccount updateCabalContributePointAccount = new CabalContributePointAccount();
            updateCabalContributePointAccount.setId(dbCabalContributePointAccount.getId());
            updateCabalContributePointAccount.setContributePoint(cabalContributePointAccount.getContributePoint() + dbCabalContributePointAccount.getContributePoint());
            updateCabalContributePointAccount.setUpdateTime(new Timestamp(new Date().getTime()));
            DynamicDataSourceContextHolder.setContextKey(SystemConstants.DS_KEY_CABALMALL);
            cabalContributePointAccountMapper.updateById(updateCabalContributePointAccount);
        }
    }


}
